/**
 * IndexToken.java
 *
 * @author: ZhuJiahui
 * @date: 2019/1/7 21:38
 */
package com.zhujiahui.nlp.core.domain;

/**
 * @description:
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class IndexToken implements Cloneable {

	// Token对象
	private Token token = null;

	// 在文本中的索引位置
	private int index = 0;

	public static final int DEFAULT_INDEX = 0;

	// toString方法的分隔符
	public static final String DELIMITER = "||";

	/**
	 * 默认构造方法
	 */
	public IndexToken() {
		this.token = new Token();
		this.index = DEFAULT_INDEX;
	}

	/**
	 * 带参数的构造函数
	 * 
	 * @param token
	 *            token对象
	 * @param index
	 *            索引
	 */
	public IndexToken(Token token, int index) {
		super();
		this.token = token;
		this.index = index;
	}

	/**
	 * 带参数的构造函数
	 * 
	 * @param word
	 *            词汇
	 * @param pos
	 *            词性
	 * @param index
	 *            索引
	 */
	public IndexToken(String word, String pos, int index) {
		super();
		this.token = new Token(word, pos);
		this.index = index;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((token == null) ? 0 : token.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IndexToken other = (IndexToken) obj;
		if (token == null) {
			if (other.token != null)
				return false;
		} else if (!token.equals(other.token))
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.token.getWord());
		sb.append(Token.DELIMITER);
		sb.append(this.token.getPos());
		sb.append(DELIMITER);
		sb.append(String.valueOf(this.index));
		return sb.toString();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	@Override
	protected Object clone() throws CloneNotSupportedException {
		IndexToken clonedIndexToken = null;
		try {
			clonedIndexToken = (IndexToken) super.clone();
			
			if (this.token != null)
			{
				clonedIndexToken.token = (Token) this.token.clone();
			}
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return clonedIndexToken;
	}
	
	/**
	 * ==================================================== 
	 * 以下是get和set方法
	 */

	/**
	 * @return the token
	 */
	public Token getToken() {
		return token;
	}

	/**
	 * @param token the token to set
	 */
	public void setToken(Token token) {
		this.token = token;
	}

	/**
	 * @return the index
	 */
	public int getIndex() {
		return index;
	}

	/**
	 * @param index the index to set
	 */
	public void setIndex(int index) {
		this.index = index;
	}

}
